package msu.erdc.hci;

import msu.erdc.hci.ContactsAdapter.ContactListener;
import msu.erdc.hci.customObjects.ContactObject;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactList extends ListFragment implements ContactListener   {
	
	ImageView im;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.contact_list, container, false);
        return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //do stuff here
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{

		super.onActivityCreated(savedInstanceState);
		//ListAdapter adapter = new ArrayAdapter<ContactObject>(getActivity(), android.R.layout.simple_list_item_1, ((Global)getActivity().getApplication()).contacts);
		ContactsAdapter adapter = new ContactsAdapter(getActivity(),R.layout.contact_list_row, ((Global)getActivity().getApplication()).contacts, this );
		getListView().addHeaderView(createHeader());
		setListAdapter(adapter);
	}
	
	@Override
	public void onDestroyView()
	{
		setListAdapter(null);
		super.onDestroyView();
	}
	
	private View createHeader()
	{
		LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	TextView tv = (TextView)inflater.inflate(android.R.layout.simple_list_item_1, getListView(), false);
//    	if(AContact.category != null)
//    	{
//    		tv.setText("Emergency Contacts"+AContact.category);
//    	}
//    	else
//    	{
    		tv.setText("Emergency Contacts");
//    	}
    	tv.setGravity(Gravity.CENTER);
    	return tv;
	}

	@Override
	public void name_click(int index) {
		ContactDetail detailFrag = new ContactDetail();
		Bundle arguments = new Bundle();
		arguments.putInt("index", index);
		detailFrag.setArguments(arguments);
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(android.R.id.content, detailFrag, "contactdetail");
		ft.addToBackStack(null);
		ft.commit();
		
	}

	@Override
	public void email_click(ContactObject c) {
		//email code
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		emailIntent.setType("message/rfc822");
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Accident Info");
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "email body");
		startActivity(Intent.createChooser(emailIntent, "Send Email:"));  
		
	}

	@Override
	public void sms_click(ContactObject c) {
		// TODO Auto-generated method stub
		Uri uri = Uri.parse(c.mobile);
		Intent textIntent = new Intent(Intent.ACTION_SENDTO,uri);
		textIntent.putExtra("sms+body", "sms text");
		startActivity(Intent.createChooser(textIntent, "Send Text:"));
		
	}

	@Override
	public void call_click(ContactObject c) {
		// TODO Auto-generated method stub
		try {

		       Intent callIntent = new Intent(Intent.ACTION_CALL);
		       callIntent.setData(Uri.parse("tel:" + c.phone));
		       startActivity(callIntent);
		       } catch (ActivityNotFoundException e) {
		       Log.e("helloandroid dialing example", "Call failed", e);
		       }
		
	}
	
	
}

